package base.httpclient.demo00;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * jdk发送HTTP请求原生API演示(可以比较下与 apache的 HttpClients 发展趋势)
 */
public class HttpRequestCase {
    public static void main(String[] args) {
        String baidu = "https://www.baidu.com";
        try {
            URL url = new URL(baidu);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            try (// 包装字符流，可整行读取
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(
                                connection.getInputStream(), StandardCharsets.UTF_8))){
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
